#include "view/numview.h"
#include <QLayout>
#include <QToolButton>
#include <QDebug>

NumView::NumView(QWidget *parent) :
    QWidget(parent)
{

    QToolButton *numberLetterBtn[16];

    QStringList letterLowList;
    letterLowList << "1" << "2" << "3" << "删除" << "4" << "5" << "6" << "." << "7" << "8" << "9" << "@" << "返回" << "0" << "空格" <<"确认";

    for (int var = 0; var < 16; ++var) {
        numberLetterBtn[var] = new QToolButton();
        numberLetterBtn[var]->setToolButtonStyle(Qt::ToolButtonTextOnly);
        numberLetterBtn[var]->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
        numberLetterBtn[var]->setText(letterLowList.at(var));
        connect(numberLetterBtn[var],&QToolButton::clicked,this,[=]()->void{clickBtnSlot();});
    }

    QVBoxLayout *globalVLayout = new QVBoxLayout(this);
    globalVLayout->setContentsMargins(8, 2, 8, 8);
//    globalVLayout->setMargin(2);
    globalVLayout->setSpacing(0);

    QHBoxLayout *globalHLayout[4];

    for (int i = 0; i < 4; ++i) {
        globalHLayout[i] = new QHBoxLayout();
        globalHLayout[i]->setSpacing(0);
        for (int j = 0; j < 4; ++j) {
            globalHLayout[i]->addWidget(numberLetterBtn[i*4+j]);
        }
        globalVLayout->addLayout(globalHLayout[i]);
    }

}


void NumView::clickBtnSlot()
{
    QToolButton *clickedBtn = qobject_cast<QToolButton *>(sender());//获取信号发送者的对象
    QString str = clickedBtn->text();
    NumView *btn = new NumView;
    if(str == "空格")
        str = " ";
    if(str == "删除")
        emit btn->deleteText();
    else if(str == "确认")
        emit btn->enterTest();
    else if(str == "返回")
        emit btn->returnTest();
    else
        emit btn->numTest(str);

    qDebug() << "输出：" << str;
}



